package com.design.fitment.adapter;

/**
 * Created by 娇哥 on 2016/1/18.
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.fitment.R;
import com.design.fitment.app.MyApplication;
import com.design.fitment.entity.MoreList;
import com.design.fitment.view.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class MoreListAdapter extends BaseAdapter {

    private List<MoreList.DataEntity> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    public MoreListAdapter(Context context,List<MoreList.DataEntity> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = objects;
        imageLoader = MyApplication.getApp().getImageLoader();
        options = MyApplication.getApp().getImageOptions();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public MoreList.DataEntity getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.more_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivMianList = (ImageView) convertView.findViewById(R.id.iv_mian_list);
            viewHolder.ivMianListRound = (RoundImageView) convertView.findViewById(R.id.iv_mian_list_round);
            viewHolder.tv1MainList = (TextView) convertView.findViewById(R.id.tv1_main_list);
            viewHolder.tv2MainList = (TextView) convertView.findViewById(R.id.tv2_main_list);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        imageLoader.displayImage(objects.get(position).getImgUrl(), viewHolder.ivMianList, options);
        imageLoader.displayImage(objects.get(position).getFacePic(), viewHolder.ivMianListRound, options);
        viewHolder.tv1MainList.setText(objects.get(position).getName());
        if(objects.get(position).getStyleName()!=null){
            viewHolder.tv2MainList.setText(objects.get(position).getAreaName()+"/"
                    +objects.get(position).getStyleName()+"/"+objects.get(position).getSpaceName()+"/"+objects.get(position).getCounts()+"图");
        }else{
            viewHolder.tv2MainList.setText(objects.get(position).getAreaName()+"/"
                    +objects.get(position).getSpaceName()+"/"+objects.get(position).getCounts()+"图");
        }
        Log.d("getView", "getView() returned: " + objects.get(position).getImgUrl());
        return convertView;
    }



    protected class ViewHolder {
        private ImageView ivMianList;
        private RoundImageView ivMianListRound;
        private TextView tv1MainList;
        private TextView tv2MainList;
    }
}
